package cn.itcast.work.service.user_service;

import cn.itcast.work.dao.system.SystemMapper;
import cn.itcast.work.dao.user.UserMapper;
import cn.itcast.work.pojo.CUser;
import cn.itcast.work.pojo.InfoText;
import cn.itcast.work.pojo.TextWhole;
import cn.itcast.work.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.io.IOException;
import java.util.List;

/**
 * 业务层 用户管理
 */
@Service("userService")
public class UserService {
    //    //调用dao层
//    @Resource(name = "userDao")
//    private UserDao userDao;
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户注册
     */
    @Transactional
    public boolean toRegister(User user) throws IOException {
        System.out.println("注册 service");
        //调用dao层
        int i = userMapper.toAdd(user);//执行注册
        return i > 0;
    }

    @Transactional
    //测试邮箱
    public boolean testEmail(String v) {
        System.out.println("账号是否存在");
        Integer result = userMapper.toSelectE(v);
        boolean exists = result != null && result == 1;//  为真存在 为假不存在
        return exists;
    }

    //测试号码
    public boolean testPhone(String v) {
        System.out.println("账号是否存在");
//      直接返回的值是数字 1 或 null 其解释为布尔值
        Integer result = userMapper.toSelectP(v);
        boolean exists = result != null && result == 1;// 为真存在 为假不存在
        return exists;
    }

    /**
     * 用户登录
     */
    public User toLogin(String phone, String pd) {
        System.out.println("登录 service");
        return userMapper.toSelectU(phone, pd);
    }

    /**
     * 注销用户
     */
//    @Resource(name = "systemDao")
//    private SystemDaoImpl systemDao;

    @Autowired
    private SystemMapper systemMapper;

    @Transactional
    public boolean toDeleteUser(String phone) {
        System.out.println("注销 service");
        System.out.println("注销用户 dao");
        //从数据库中删除user表 picture表 texts的表（相关文章） username的值 phone的值 pic的值
        System.out.println("删除 相关表 dao");
        userMapper.deleteText(phone);
        userMapper.deleteTextP(phone);
        userMapper.deleteMotto(phone);

        int result = userMapper.toDelete(phone);

        return result > 0;
    }

    //根据form标签
    //1.显示所有数据
    public CUser showInf(String phone) {
        System.out.println("显示修改信息 service");
        CUser cUser = userMapper.selectInf(phone);
        System.out.println(cUser);
        return cUser;
    }

    //查询号码
    public String selectPhone(String author) {
        System.out.println("查询号码 controller");
        return userMapper.selectPhone(author);
    }

    /**
     * 修改个人信息
     */
    @Transactional
    public boolean toUpdateUser(String username, String password, String phone) {
        System.out.println("修改个人信息 service");
        //更新user表
        int i = userMapper.updateUser(username, password, phone);
        //更新home_user表
        int upuers = userMapper.updateHome_user(username, phone);
        return i > 0;
    }

    /**
     * 修改个性签名
     */
    public boolean toUpdateMotto(String motto, String phone) {
        System.out.println("修改个签  service");
        int i = userMapper.updateMotto(motto, phone);
        return i > 0;
    }

    /**
     * 在用户登录时查询个签
     */
    public String toSelectMotto(String phone) {
        System.out.println("查询个签 service");
        return userMapper.selectMotto(phone);
    }

    /**
     * 在用户注册时插入个签
     */
    public boolean toInsertMotto(String phone) {
        System.out.println("插入个签 service");
        int i = userMapper.insertMotto(phone, "您还没有设置个签");
        return i > 0;
    }

    /**
     * 初始化图片insert
     */
    public boolean toInsertPic(String phone) {
        System.out.println("上传图片 service");
        int i = userMapper.InsertPic(phone, "girl.jpg");
        return i > 0;
    }

    /**
     * update图片 修改图片
     */
    public boolean toUpdatePic(String phone, String path) {
        System.out.println("修改图片 service");
        int i = userMapper.UpdatePic(phone, path);
        return i > 0;
    }

    /**
     * 查找
     */
    public String toSelectPic(String phone) {
        System.out.println("返回图片路径 service");
        return userMapper.SelectPic(phone);
    }

    /**
     * 存博客
     * 保存时间和文章
     */
    public void toSave(TextWhole text) {
        System.out.println("保存博客 service");
        userMapper.insertText(text);
    }

    //查看自己博客 根据用户和创作时间
    public TextWhole toSee(String title) {
        System.out.println("查询文章 service");
        return userMapper.selectText(title);
    }

    //查看用户所有博客
    public List<TextWhole> toSeeAll(String phone) {
        System.out.println("用户所有博客 service");
        List<TextWhole> textWholeList = userMapper.selectAllText(phone);
//        textWholeList.forEach(text -> System.out.println(text.getTitle() + text.getCreatedate()));
        return textWholeList;
    }


    @Transactional
    //删除指定title的博客
    public boolean toDelete(String name) {
        System.out.println("删除博客 service");
        //删除博客
        //撤销 home_page的表
        systemMapper.cancelText(name);
        //删除text
        int re = userMapper.deleteText(name);

        return re > 0;
    }

    @Transactional
    //更新博客
    public boolean toUpDate(TextWhole textWhole, String phone) {
//        System.out.println("更新 service");
        //拿到id值
//        System.out.println("需要的更新id");
//        System.out.println(textWhole.getTitle());
        int id = userMapper.selectId(textWhole.getTitle());
//        System.out.println(id);
        //更新博客表
        int re = userMapper.update(textWhole, id);
        //首页表也需要更新文章名
//        int up = userMapper.updateHome_page(textWhole.getTitle(), phone);
        return re > 0;
    }

    //查询用户名
    public String selectUsername(String phone) {
        System.out.println("查询用户表 名 service");
        return userMapper.selectUserName(phone);
    }

    //查询分类表
    public InfoText selectInf(String title) {
//        System.out.println("查询 分类 service");
        return userMapper.selectTextInf(title);
    }
}
